<template>
  <div id="attendCalendar">
    <el-row :gutter="5">
      <el-col :span="14" :offset="4" :xs="24">
        <el-card class="calendarCard">
          <div id="button">
            <el-button @click="skip('preYear')" type="primary" round size="mini"><i class="el-icon-arrow-left"></i>年
            </el-button>
            <el-button @click="skip('preMonth')" type="warning" round size="mini"><i class="el-icon-arrow-left"></i>月
            </el-button>
            <el-button @click="skip('preDay')" type="success" round size="mini"><i class="el-icon-arrow-left"></i>日
            </el-button>
            <el-button @click="skip('today')" type="info" round size="mini">今天</el-button>
            <el-button @click="skip('postDay')" type="success" round size="mini">日<i class="el-icon-arrow-right"></i>
            </el-button>
            <el-button @click="skip('postMonth')" type="warning" round size="mini">月<i class="el-icon-arrow-right"></i>
            </el-button>
            <el-button @click="skip('postYear')" type="primary" round size="mini">年<i class="el-icon-arrow-right"></i>
            </el-button>
          </div>
          <br />
          <el-calendar :first-day-of-week=7 v-model="value">
            <template slot="dateCell" slot-scope="{date, data}">
              <div slot="reference" class="div-Calendar" @click="clickCalendar(data)"
                style="position: relative;z-index: 10">
                <!-- <p>{{ data.day.split('-').slice(2).join('-') }}</p> -->
                <div>
                  <font size="4px">{{ data.day.split('-').slice(2).join('-') }}</font>
                  <!-- <font  color="blue" size="1px" > {{ holiday[data.day.split('-').slice(1).join('-')]  ?  holiday[data.day.split('-').slice(1).join('-')]: ''}} </font> -->
                  <!-- <font  color="red" size="1px" > {{ workday[data.day.split('-').slice(1).join('-')]  ?  workday[data.day.split('-').slice(1).join('-')]: ''}} </font> -->
                  <!-- <font  color="blue" size="1px" > {{ holiday[data.day]  ?  holiday[data.day]: ''}} </font> -->
                  <font color="blue" size="1px"> {{ holiday[data.day] ? '休' : '' }} </font>
                  <font color="red" size="1px"> {{ workday[data.day] ? "班" : '' }} </font>
                  <font color="red" size="1px"> {{ work[data.day] ? '班' : '' }} </font>

                  <!-- <font  color="blue" size="1px" >  <br/> {{ rqi.indexOf(data.day.split('-').slice(1).join('-')) > -1 ?  rqineirong[data.day.split('-').slice(1).join('-')]: ''}} </font> -->

                </div>
                <div class="center" :class="getFes(data.day) ? 'centerone' : 'centertwo'">
                  {{ getFes(data.day) }}
                </div>
              </div>
              <div v-if="data.isSelected" id="selectP"></div>
            </template>
          </el-calendar>
          <br />

        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="5">
      <el-col :span="14" :offset="4" :xs="24">
        <el-card class="calendarInfo">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="假日名称" prop="holidayName">
              <el-input v-model="queryParams.holidayName" placeholder="请输入假日名称" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="串休日期" prop="adjustmentDate">
              <el-date-picker clearable v-model="queryParams.adjustmentDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择串休日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="串休类型" prop="adjustType">
              <el-select v-model="queryParams.adjustType" placeholder="请选择串休类型" clearable>
                <el-option v-for="dict in adjustTypeOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['ehr:attendance_calender:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                v-hasPermi="['ehr:attendance_calender:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                v-hasPermi="['ehr:attendance_calender:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['ehr:attendance_calender:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="attendance_calenderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="日历记录ID" align="center" prop="calenderId" /> -->
            <el-table-column label="假日名称" align="center" prop="holidayName" />
            <el-table-column label="串休日期" align="center" prop="adjustmentDate" width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.adjustmentDate, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="串休类型" align="center" prop="adjustType" width="100" :formatter="adjustTypeFormat" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
              <template slot-scope="scope">
                <!-- <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-document"
                    @click="handleDetail(scope.row)"
                    v-hasPermi="['ehr:attendance_calender:detail']"
                  >详情</el-button> -->
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['ehr:attendance_calender:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['ehr:attendance_calender:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改考勤日历对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="假日名称" prop="holidayName">
              <el-input v-model="form.holidayName" placeholder="请输入假日名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="串休日期" prop="adjustmentDate">
              <el-date-picker clearable v-model="form.adjustmentDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择串休日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="串休类型" prop="adjustType">
              <el-select v-model="form.adjustType" placeholder="请选择串休类型">
                <el-option v-for="dict in adjustTypeOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button v-if="isDelete" type="danger" @click="handleDelete(form)">删 除</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttendance_calender, getAttendance_calender, delAttendance_calender, addAttendance_calender, updateAttendance_calender } from "@/api/ehr/attendance_calender";
import calendar from "@/utils/calendar.js";

export default {
  name: "Calendar",
  data() {
    return {
      // 假期数组
      rqi: [],
      // 假期信息
      rqineirong: {},
      workday: {},
      work: [],
      holiday: {},
      value: new Date(),

      isSelectDate: false,
      isDelete: false,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考勤日历表格数据
      attendance_calenderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 串休类型业务字典
      adjustTypeOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "adjustmentDate", // 排序字段
        isAsc: "DESC", // 升序或降序 大写的 ASC或DESC,为空代表升序
        holidayName: null,
        adjustmentDate: null,
        adjustType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        holidayName: [
          { required: true, message: "假日名称不能为空", trigger: "blur" }
        ],
        adjustmentDate: [
          { required: true, message: "串休日期不能为空", trigger: "blur" }
        ],
        adjustType: [
          { required: true, message: "串休类型不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    // 串休类型业务字典
    this.getEnumDicts("ATTEND_CAL_TYPE").then((response) => {
      this.adjustTypeOptions = response.data;
    });
    // 获取节假日信息
    this.getList();
  },
  computed: {
    selectDate() {
      // return moment(this.value).format("YYYY-MM-DD");
    }
  },
  methods: {
    /** 查询节假日管理 - 法定节假日列表 */
    getList() {
      this.loading = true;
      this.holiday = {};
      this.workday = {};
      listAttendance_calender(this.queryParams).then(response => {
        response.rows.forEach((item, index) => {
          if ("rest" === item.adjustType) {
            // this.holiday.push(item.adjustmentDate);
            this.holiday[item.adjustmentDate] = "休";
          } else {
            this.workday[item.adjustmentDate] = "班";
            // this.work.push(item.adjustmentDate);
          }
          if ("work" === item.adjustType) {
            this.work.push(item.adjustmentDate);
          }
        });

        this.attendance_calenderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      console.info("holiday:" + this.total);
    },
    getFes(solarDate) {
      var solar = solarDate.split("-");
      var lunar = calendar.solar2lunar(solar[0], solar[1], solar[2]);
      return lunar.festival ? lunar.festival : lunar.lunarFestival
    },
    skip(flag) {
      if (flag === 'preYear') this.value = new Date(this.value.setFullYear(this.value.getFullYear() - 1));
      else if (flag === 'preMonth') this.value = new Date(this.value.setMonth(this.value.getMonth() - 1));
      else if (flag === 'preDay') this.value = new Date(this.value.setDate(this.value.getDate() - 1));
      else if (flag === 'today') this.value = new Date();
      else if (flag === 'postDay') this.value = new Date(this.value.setDate(this.value.getDate() + 1));
      else if (flag === 'postMonth') this.value = new Date(this.value.setMonth(this.value.getMonth() + 1));
      else if (flag === 'postYear') this.value = new Date(this.value.setFullYear(this.value.getFullYear() + 1));
    },
    clickCalendar(data) {
      console.log(data);
      // PubSub.publish("uploadWarningResultWarningTime", data.day);

      // 根据 选中的日期 forEach   attendance_calenderList
      // 获取日期哪一行
      this.attendance_calenderList.forEach((item, index) => {
        if (data.day === item.adjustmentDate) {
          this.isDelete = true;
          this.handleUpdate(item);
        } else {
          // 考勤数据为空时，调起新增界面
          this.isDelete = false;
          this.calenderHandleAdd(data.day);
        }

      });
    },

    adjustTypeFormat(row, column) {
      let val = null;
      this.adjustTypeOptions.forEach((item, index) => {
        if (row.adjustType === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        calenderId: null,
        holidayName: null,
        adjustmentDate: null,
        adjustType: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.calenderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    calenderHandleAdd(date) {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      if (date) {
        this.isSelectDate = true;
        this.form.adjustmentDate = date;
      } else {
        this.isSelectDate = false;
      }
      this.open = true;
      this.title = "添加考勤日历";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加考勤日历";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const calenderId = row.calenderId || this.ids
      getAttendance_calender(calenderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "考勤日历详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const calenderId = row.calenderId || this.ids
      getAttendance_calender(calenderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考勤日历";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.calenderId != null && this.updateFlag) {
            updateAttendance_calender(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAttendance_calender(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.getList();
              this.open = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const calenderIds = row.calenderId || this.ids;
      this.$modal.confirm('是否确认删除考勤日历编号为"' + calenderIds + '"的数据项？').then(function () {
        return delAttendance_calender(calenderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
      this.open = false;

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/attendance_calender/export', {
        ...this.queryParams
      }, `attendance_calender_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
#attendCalendar {
  width: 100%;
  height: 100%;

  #button {
    margin-top: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  #selectP {
    width: 30px;
    height: 30px;
    background-color: #15ff2c;
    position: absolute;
    border-radius: 50%;
    opacity: 0.6;
  }

  ::v-deep .el-calendar__header {
    // 修改头部背景颜色
    background-color: #57617C;
    padding: 3px 5px;
    border: none;
    display: flex;
    justify-content: center;

    .el-calendar__button-group {
      // 隐藏原生按钮
      display: none;
    }

    .el-calendar__title {
      // 修改头部标题的字体颜色
      color: white !important;
      font-size: 18px;
      font-weight: bolder;
    }
  }

  ::v-deep .el-calendar__body {
    // 修改主题部分
    padding: 0;
  }

  ::v-deep .el-calendar-table {
    thead {
      th {
        // 修改头部星期部分
        padding: 0;
        background-color: #57617C;
        color: white;
      }
    }

    .is-selected {
      .el-calendar-day {
        p {
          // 选中日期颜色
          color: black;
        }
      }
    }

    .el-calendar-day {
      // 每天小块样式设置
      padding: 0;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;

      p {
        // 所有日期颜色
        color: black;
        z-index: 1;
        user-select: none;
        display: flex;
      }
    }
  }

  ::v-deep .el-calendar-table__row {

    .prev,
    .next {

      // 修改非本月
      .el-calendar-day {
        p {
          color: #f0d9d5;
        }
      }
    }

    td {

      // 修改每一个日期td标签
      &:first-child,
      &:last-child {
        background-color: #f5f5f5;
      }
    }
  }

  button {
    padding: 3px 10px;
  }

  .centerone {
    background: #F0F5F1;
    color: #2410d3;
  }

  .centertwo {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
  }
}
</style>

